using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;



public class NewBehaviourScript : MonoBehaviour
{
    public Texture2D O;
    public Texture2D X;
    GUIStyle myStyle;
    private int turn = 1;
    /* turn=1为✓，turn==2为× */
    int[][] mess = new int[3][] { new int[3], new int[3], new int[3] };


    void reset()
    {
        O = AssetDatabase.LoadAssetAtPath("Assets/1.jpg", typeof(Texture2D)) as Texture2D;
        X = AssetDatabase.LoadAssetAtPath("Assets/0.jpg", typeof(Texture2D)) as Texture2D;
        turn = 1;
        for (int i = 0; i < 3; ++i)
        {
            for (int j = 0; j < 3; ++j)
            {
                mess[i][j] = 0;
            }
        }
        Debug.Log("Game Reset");
    }




    int check()
    {
        for (int i = 0; i < 3; i++)
        {
            if (mess[i][0] != 0 && mess[i][0] == mess[i][1] && mess[i][0] == mess[i][2])
            {
                return mess[i][0];
            }
        }
        for (int i = 0; i < 3; i++)
        {
            if (mess[0][i] != 0 && mess[0][i] == mess[1][i] && mess[0][i] == mess[2][i])
            {
                return mess[0][i];
            }
        }
        if (mess[1][1] != 0 &&
            mess[0][0] == mess[1][1] && mess[2][2] == mess[1][1] ||
            mess[0][2] == mess[1][1] && mess[2][0] == mess[1][1])
        {
            return mess[1][1];
        }
        /*未结束*/
        for (int i = 0; i < 3; ++i)
        {
            for (int j = 0; j < 3; ++j)
            {
                if (mess[i][j] == 0) return 0;
            }
        }
        /*棋局已满*/
        return 3;
    }

    /*动态显示*/
    void display(int res)
    {
        int m = Screen.width / 2;
        if (res == 0)
        {
            if (turn == 1)
                GUI.Box(new Rect(m - 50, 85, 100, 35), "it is ✓ turn");
            else
                GUI.Box(new Rect(m - 50, 85, 100, 35), "it is X turn");
        }
        else if (res == 1)
        {
            GUI.Box(new Rect(m - 50, 85, 100, 35), "✓ WIN");
        }
        else if (res == 2)
        {
            GUI.Box(new Rect(m - 50, 85, 100, 35), "X WIN");
        }
        else if (res == 3)
        {
            GUI.Box(new Rect(m - 50, 85, 100, 35), "Draw");
        }
    }

    void OnGUI()
    {
        int m = Screen.width / 2;
        GUI.Box(new Rect(m - 150, 70, 300, 420), "井字棋");
        /*如果reset被点击则重开*/
        if (GUI.Button(new Rect(m - 50, 130, 100, 35), new GUIContent("Reset", "点击重新开始"))) reset();
        GUI.Label(new Rect(m - 150, 170, 150, 35), GUI.tooltip);
        int res = check();
        for (int i = 0; i < 3; ++i)
        {
            for (int j = 0; j < 3; ++j)
            {
                if (mess[i][j] == 1)
                {
                    GUI.Button(new Rect(m - 150 + i * 100, 170 + j * 100, 100, 100), O);
                }
                else if (mess[i][j] == 2)
                {
                    GUI.Button(new Rect(m - 150 + i * 100, 170 + j * 100, 100, 100), X);
                }
                if (GUI.Button(new Rect(m - 150 + i * 100, 170 + j * 100, 100, 100), ""))
                {
                    if (res == 0)
                    {
                        if (turn == 1)
                        {
                            mess[i][j] = turn;
                            turn = 2;
                        }
                        else
                        {
                            mess[i][j] = turn;
                            turn = 1;
                        }
                    }
                }
            }
        }
        display(res);
    }
    // Start is called before the first frame update
    void Start()
    {
        reset();
    }

    // Update is called once per frame
    void Update()
    {

    }
}
